package com.qf.taobei.controller;

import com.qf.taobei.entity.*;
import com.qf.taobei.entity.vo.ProductVo;
import com.qf.taobei.service.OrderService;
import com.qf.taobei.service.ProductService;
import com.qf.taobei.service.ProductTypeService;
import com.qf.taobei.service.UserService;
import com.qf.taobei.entity.vo.ViewDiscount;
import com.qf.taobei.service.*;
import com.qf.taobei.utils.OssUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private DiscountService discountService;

    @Autowired
    private AdminService adminService;

    @GetMapping("/login")
    public RespBean login(HttpSession session, String username, String password){
        return adminService.adminLogin(session,username,password);
    }

    @PostMapping("/logOut")
    public RespBean logOut(HttpSession session){
        session.removeAttribute("admin");
        return RespBean.respSuccess("退出登录");
    }

    @GetMapping("/getUserList")
    public RespBean queryUserList(){
        return RespBean.respSuccess("获取用户列表成功",userService.queryUserList());
    }

    @GetMapping("/queryUsers")
    public RespBean queryUsers(String userInfo){
        List<User> users = userService.searchUserByuserInfo(userInfo);
        return RespBean.respSuccess("查询用户列表成功",users);
    }

    @GetMapping("/getUserById")
    public RespBean getUserById(String id){
        User userById = userService.getUserById(Integer.parseInt(id));
        return RespBean.respSuccess("根据id查询用户成功",userById);
    }

    @PostMapping("/updateUser")
    public RespBean updateUser(User user){
        Integer status = userService.updateInfo(user);
        return RespBean.respSuccess("更新用户成功",status);
    }

    @PostMapping("/deleteUser")
    public RespBean deleteUser(String id){
        Integer status = userService.delUser(Integer.parseInt(id));
        return RespBean.respSuccess("删除用户成功",status);
    }

    @GetMapping("/getProductListByFlag")
    public RespBean getProductList(String flag){
        if ("3".equals(flag)){
            return productService.getProductListByFlag("3");
        }else {
            return productService.getProductListByFlag("");
        }
    }

    @GetMapping("/queryProductByProductInfo")
    public RespBean queryProductByProductInfo(Dim dim){
        RespBean respBean = productService.queryProductByDim(dim);
        List<ProductVo> list = (List<ProductVo>) respBean.getData();
        List<ProductVo> newList = new ArrayList<>();
        for (ProductVo productVo : list) {
            if (productVo.getFlag() != 3){
                newList.add(productVo);
            }
        }
        return RespBean.respSuccess("查询到的商品如下",newList);
    }

    @PostMapping("/deleteProduct")
    public RespBean deleteProduct(String id){
        RespBean respBean = productService.delProductById(Long.parseLong(id));
        return respBean;
    }

    @GetMapping("/getOrderList")
    public RespBean getOrderList(){
        return  orderService.getOrderList();
    }

    @GetMapping("/queryOrderByOrderNumber")
    public RespBean queryOrders(String orderNumber){
        List<Order> orders = orderService.queryOrderByOrderNumber(orderNumber);
        return RespBean.respSuccess("成功",orders);
    }

    @PostMapping("/deleOrder")
    public RespBean deleOrder(Integer id){
        Integer integer = orderService.deleteOrderByOrderId(id);
        return RespBean.respSuccess("成功",1);
    }

    @GetMapping("/getProductTypeList")
    public RespBean getProductTypeList(){
        return productTypeService.queryAllProductType();
    }

    @GetMapping("/getStoreByName")
    public RespBean getStoreByName(String name){
        return storeService.getStoreByName(name);
    }

    @GetMapping("/getStoreList")
    public RespBean getStoreList(){
        return storeService.selectAllStore();
    }

    @PostMapping("/delStore")
    public RespBean delStore(Integer id){
        return storeService.deleteStoreByStoreIdFalse(id);
    }

    @GetMapping("/getDiscountList")
    public RespBean selectAllDiscount(){
        List<ViewDiscount> viewDiscounts = discountService.queryAllDiscount();
        return RespBean.respSuccess("成功",viewDiscounts);
    }

    @GetMapping("/queryStoreByFlag")
    public RespBean queryStoreByFlag(Integer flag){
        List<Store> stores = storeService.queryStoreByFlag(flag);
        return RespBean.respSuccess("成功",stores);
    }

    @PostMapping("/updateStoreFlag")
    public RespBean updateStoreFlag(Integer id){
        Integer integer = storeService.updateStoreFlagById(id);
        if (integer == 1){
            return RespBean.respSuccess("成功",integer);
        }
        return RespBean.respError("失败");
    }

    @PostMapping("updateProductFlag")
    public RespBean updateProductFlag(Integer id,Integer flag){
        return productService.updateProductFlag(id,flag);
    }

    @PostMapping(value = "/upload",consumes = "multipart/*",
            headers = "content-type=multipart/form-data")
    public RespBean upload(MultipartFile iFile) throws IOException {
        String upload = OssUtils.upload(iFile.getOriginalFilename(), iFile.getInputStream());
        return RespBean.respSuccess(upload);
    }


}
